using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using AIFSCMSClient;
using AIFSContentManagementSystem.Articles;
using AIFSContentManagementSystem.Criteria;
using AIFSContentManagementSystem.Articles.Messages;
using AIFSContentManagementSystem.Criteria.Messages;

#if VS2005
    //using UserOfAIFS2005.AIFSCMSService;
#else
    //using UserOfAIFS.AIFSCMSService;
#endif

namespace UserOfAIFS
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Use the rule builder
            //RuleExpressionBuilder ruleBuilder = ExpessionBuilderFactory.GetRuleExpressionBuilder();
            //string[] exp = ruleBuilder.CreateStringCriteria("COUNTRY").IsEqualTo("US").AndNumericCriteria("POLICY_YEAR").IsGreaterThanEqualTo("2011").CriteriaExpression;

            //AIFSCMSServiceProvider provider = new AIFSCMSServiceProvider();
            //provider.Timeout = 5000000;

            GetFilteredArticleListRequest request = new GetFilteredArticleListRequest();
            request.ArticleType = ArticleType.PROMO;
            request.ProgramName = "COLLEGE";
            request.FlaggedAsActive = true;

            FactExpressionBuilder builder = ExpessionBuilderFactory.GetFactExpressionBuilder();
            
            string[] expression = builder.CreateStringCriteria("COUNTRY").IsEqualTo("US")
                .AndNumericCriteria("POLICY_YEAR").IsEqualTo("2011").CriteriaExpression;

            Criterion criteria = new Criterion();
            criteria.CriteriaExpressionArray = expression;

            Criterion orCountryCriteria = new Criterion();
            expression = builder.CreateStringCriteria("SPONSOR").IsEqualTo("468")
                .AndDateCriteria("POLICY_YEAR").IsEqualTo("2011").CriteriaExpression;

            orCountryCriteria.CriteriaExpressionArray = expression;

            List<Criterion> criteriaList = new List<Criterion>();
            criteriaList.Add(criteria);
            criteriaList.Add(orCountryCriteria);
            request.Criteria = criteriaList;

            AIFSCMSFacade.AifsCmsFacade facade = new AIFSCMSFacade.AifsCmsFacade();
            
            GetFilteredArticleListResponse result = (GetFilteredArticleListResponse)facade.ProcessArticleRequest(request);
            if (result.HasError)
            {
                //Response.Write(String.Join(".", result.ErrorMessage));
                return;
            }

            if (result.HasDataInReturn)
            {
                rptrPromo.DataSource = result.ArticleList[0].Fragments;
                rptrPromo.DataBind();
            }
        }
    }
}
